package com.cow.controller;

import com.cow.entity.AIRequest;
import com.cow.entity.AIResponse;
import com.cow.service.impl.AIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: 陈正彬
 */
@RestController
@RequestMapping("/api/ai")
@Validated
public class AIController {

    @Autowired
    private AIService aiService;

    @PostMapping("/ask")
    public ResponseEntity<AIResponse> handleQuestion(
            @Valid @RequestBody AIRequest request,
            @RequestHeader(value = "X-Session-Id", required = false) String sessionId) {

        try {
            String answer = aiService.processQuestion(request.getQuestion(), sessionId);
            return ResponseEntity.ok(AIResponse.success(answer));
        } catch (Exception e) {
            return ResponseEntity.status(500)
                    .body(AIResponse.error("客服服务暂不可用，请稍后再试"));
        }
    }
}
